# --------------------------------------------------------
# Buiilding portfolio 
# --------------------------------------------------------
# Written: Gianrocco Mucedero 
# IUSS Pavia/EUCENTRE/
# Created: 7 10 2019
# Units MUST be in kN, m 

wipe;									# Command used to start over without having to exit and restart the interpreter
puts "Model Generation Started";			# Text seen on interpreter
set outsdir "Result"
file mkdir $outsdir
file mkdir $outsdir/props

# --------------------------------------
# LOAD SOME SCRIPTS
# --------------------------------------
source Units.tcl
source joint_behaviour.tcl
source infill.tcl
source rcBC_nonDuct.tcl

# DEFINE MODEL DIMENSIONS AND DOFS
# --------------------------------------
model basic -ndm 2 -ndf 3
# --------------------------------------
# DEFINE SOME VARIABLES
# --------------------------------------
set cv		[expr 20*$mm];			# Cover
set dbL		[expr 14*$mm];			# Long Bars
set dbV		[expr 8*$mm];			# Trans Bars
set fyL		350.00;
set fuL		[expr 1.4*$fyL];
set fyV		350.00;
set Es		200000.;
set fc		20.00;
set Ec		[expr 3320*sqrt($fc)+6900];
set conc 	[list $fc $Ec $cv];
set brs 	[list $dbL $dbV]; 			# bars		Bar dias (dbL dbV) m
set STb		1;
set STc		1;
set H       3.00; #[m]
set Lcamp1   4.00
set Lcamp2   4.00

node 11   0.00 0.00 -mass 0.0 0.0 0.0; 
node 12   4.00 0.00 -mass 0.0 0.0 0.0; 
node 13   8.00 0.00 -mass 0.0 0.0 0.0; 
node 14   12.00 0.00 -mass 0.0 0.0 0.0; 

fix 11 1  1  1   
fix 12 1  1  1 
fix 13 1  1  1  
fix 14 1  1  1  


set hb1 [expr 0.40*0.5]
set hc1 [expr 0.30*0.5]

geomTransf PDelta	1 -jntOffset 0.0 $hb1 0.0 -$hb1; # z is in -X
geomTransf Linear 	3 -jntOffset $hc1 0.0 -$hc1 0.0; # # z is in Y

set GTcol1 	1
set GTbmx1 	3




#Column 
set rho_shr1 		0.002234021
set rho_top1zz1 	0.003665191
set rho_mid1zz1 	0.00
set rho_bot1zz1 	0.003665191
set rho_top2zz1 	0.003665191
set rho_mid2zz1 	0.00
set rho_bot2zz1		0.003665191
set rho_top1yy1 	0.003665191
set rho_mid1yy1 	0.00
set rho_bot1yy1 	0.003665191
set rho_top2yy1 	0.003665191
set rho_mid2yy1 	0.00
set rho_bot2yy1		0.003665191
#Beam1
set rho_shr2 		0.002234021
set rho_top1zz2 	0.002700667
set rho_mid1zz2 	0.00
set rho_bot1zz2 	0.002700667
set rho_top2zz2 	0.005401335
set rho_mid2zz2 	0.00
set rho_bot2zz2		0.002700667
set rho_top1yy2 	0.002700667
set rho_mid1yy2 	0.00
set rho_bot1yy2 	0.002700667
set rho_top2yy2 	0.005401335
set rho_mid2yy2 	0.00
set rho_bot2yy2		0.002700667




#Beam2
set rho_shr3 		0.002234021
set rho_top1zz3 	0.002700667
set rho_mid1zz3 	0.00
set rho_bot1zz3 	0.002700667
set rho_top2zz3 	0.002700667
set rho_mid2zz3 	0.00
set rho_bot2zz3		0.002700667
set rho_top1yy3 	0.002700667
set rho_mid1yy3 	0.00
set rho_bot1yy3 	0.002700667
set rho_top2yy3 	0.002700667
set rho_mid2yy3 	0.00
set rho_bot2yy3		0.002700667
#Beam4

set rho_shr5 		0.002234021
set rho_top1zz5 	0.002700667
set rho_mid1zz5 	0.00
set rho_bot1zz5 	0.002700667
set rho_top2zz5	    0.004051001
set rho_mid2zz5 	0.00
set rho_bot2zz5		0.002700667
set rho_top1yy5	    0.002700667
set rho_mid1yy5 	0.00
set rho_bot1yy5 	0.002700667
set rho_top2yy5 	0.004051001
set rho_mid2yy5 	0.00
set rho_bot2yy5     0.002700667
#Beam6
set rho_shr7 		0.002234021
set rho_top1zz7 	0.004051001
set rho_mid1zz7 	0.00
set rho_bot1zz7 	0.002700667
set rho_top2zz7	    0.002700667
set rho_mid2zz7 	0.00
set rho_bot2zz7		0.002700667
set rho_top1yy7	    0.004051001
set rho_mid1yy7 	0.00
set rho_bot1yy7 	0.002700667
set rho_top2yy7 	0.002700667
set rho_mid2yy7 	0.00
set rho_bot2yy7     0.002700667
#Beam5
set rho_shr6 		0.002234021
set rho_top1zz6 	0.004051001
set rho_mid1zz6 	0.00
set rho_bot1zz6 	0.002700667
set rho_top2zz6	    0.004051001
set rho_mid2zz6 	0.00
set rho_bot2zz6		0.002700667
set rho_top1yy6 	0.004051001
set rho_mid1yy6 	0.00
set rho_bot1yy6 	0.002700667
set rho_top2yy6 	0.004051001
set rho_mid2yy6 	0.00
set rho_bot2yy6     0.002700667
set pflag2 1

set pfile_joint [open $outsdir/props/joint.txt w];
set pfile_columns [open $outsdir/props/columns.txt w];
set pfile_beam [open $outsdir/props/beams.txt w];

joint_behaviour 2 21 0.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 91.20 $H $pfile_joint
joint_behaviour 1 22 4.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 182.40 $H $pfile_joint
joint_behaviour 1 23 8.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 182.40 $H $pfile_joint
joint_behaviour 2 24 12.00  3.00 300 300 400 300 $fc $cv $dbL $dbV 91.20 $H $pfile_joint

joint_behaviour 2 31 0.00  6.00 300 300 400 300 $fc $cv $dbL $dbV 35.75 $H $pfile_joint
joint_behaviour 1 32 4.00  6.00 300 300 400 300 $fc $cv $dbL $dbV 71.50 $H $pfile_joint
joint_behaviour 1 33 8.00  6.00 300 300 400 300 $fc $cv $dbL $dbV 71.50 $H $pfile_joint
joint_behaviour 2 34 12.00  6.00 300 300 400 300 $fc $cv $dbL $dbV 35.75 $H $pfile_joint
set pfile_infill [open $outsdir/props/infill.txt w];

infill 555 "single" [list 121 122 12 11] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;
infill 556 "single" [list 122 123 13 12] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;
infill 557 "single" [list 123 124 14 13] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;

infill 565 "single" [list 131 132 122 121] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;
infill 566 "single" [list 132 133 123 122] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;
infill 567 "single" [list 133 134 124 123] 4000 3000 300 300 400 300 0.00 0.00 $Ec 5 5 0 4 1 1 $pfile_infill ;

close $pfile_infill
#Column element
rcBC_nonDuct $STc 521 $GTcol1 11 121 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  91.20   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 522 $GTcol1 12 122 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  182.4   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 523 $GTcol1 13 123 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  182.4   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 524 $GTcol1 14 124 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  91.20   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 


rcBC_nonDuct $STc 531 $GTcol1 121 131 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  35.75   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 532 $GTcol1 122 132 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  71.50   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 533 $GTcol1 123 133 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  71.50   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 
rcBC_nonDuct $STc 534 $GTcol1 124 134 $fyL $fyV $Es $fc $Ec 0.300 0.300 0.180  $cv  $dbL $dbV  35.75   [expr 0.5*$H]  $rho_shr1 $rho_top1zz1 $rho_mid1zz1 $rho_bot1zz1 $rho_top2zz1 $rho_mid2zz1 $rho_bot2zz1 $rho_top1yy1 $rho_mid1yy1 $rho_bot1yy1 $rho_top2yy1 $rho_mid2yy1 $rho_bot2yy1 $pfile_columns $pflag2; 


#Beam element
rcBC_nonDuct $STb  721 $GTbmx1 621 622 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp1]  $rho_shr5 $rho_top1zz5 $rho_mid1zz5 $rho_bot1zz5 $rho_top2zz5 $rho_mid2zz5 $rho_bot2zz5 $rho_top1yy5 $rho_mid1yy5 $rho_bot1yy5 $rho_top2yy5 $rho_mid2yy5 $rho_bot2yy5 $pfile_beam $pflag2; 
rcBC_nonDuct $STb  722 $GTbmx1 622 623 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr6 $rho_top1zz6 $rho_mid1zz6 $rho_bot1zz6 $rho_top2zz6 $rho_mid2zz6 $rho_bot2zz6 $rho_top1yy6 $rho_mid1yy6 $rho_bot1yy6 $rho_top2yy6 $rho_mid2yy6 $rho_bot2yy6 $pfile_beam $pflag2;
rcBC_nonDuct $STb  723 $GTbmx1 623 624 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr7 $rho_top1zz7 $rho_mid1zz7 $rho_bot1zz7 $rho_top2zz7 $rho_mid2zz7 $rho_bot2zz7 $rho_top1yy7 $rho_mid1yy7 $rho_bot1yy7 $rho_top2yy7 $rho_mid2yy7 $rho_bot2yy7 $pfile_beam $pflag2; 

rcBC_nonDuct $STb  731 $GTbmx1 631 632 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp1]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2; 
rcBC_nonDuct $STb  732 $GTbmx1 632 633 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2;
rcBC_nonDuct $STb  733 $GTbmx1 633 634 $fyL $fyV $Es $fc $Ec 0.300 0.400 0.180  $cv  $dbL $dbV  0.00  [expr 0.5*$Lcamp2]  $rho_shr3 $rho_top1zz3 $rho_mid1zz3 $rho_bot1zz3 $rho_top2zz3 $rho_mid2zz3 $rho_bot2zz3 $rho_top1yy3 $rho_mid1yy3 $rho_bot1yy3 $rho_top2yy3 $rho_mid2yy3 $rho_bot2yy3 $pfile_beam $pflag2; 

close $pfile_columns
close $pfile_beam
close $pfile_joint


#set w_piano -23.29
#set w_roof  -13.85
set self_pilastri -2.16
set self_travi -2.88

# Modal Analysis
set numModes 1
for { set k 1 } { $k <= $numModes } { incr k } {
    recorder Node -file [format "modes/mode%i.out" $k] -nodeRange 1 6 -dof 1 2 3  "eigen $k"
} 
set lambda [eigen  $numModes];
set omega {}
set f {}
set T {}
set pi 3.141593
foreach lam $lambda {
	lappend omega [expr sqrt($lam)]
	lappend f [expr sqrt($lam)/(2*$pi)]
	lappend T [expr (2*$pi)/sqrt($lam)]
}
puts "Modal Analysis Completed..........."

puts [format "Period s :
Period= %.3f" $T]
set period [open $outsdir/period.txt w];
puts $period [format "%.5f" $T]
close $period
# Load 	
#set w_piano -23.29
#set w_roof  -13.85
set self_pilastri -2.16
set self_travi -2.88
#8.2875 4.8075

pattern Plain 101 Constant {
	############# ELEVATION 2 #############
	load 121  	0.0 -91.20   0.0
	load 122  	0.0 -182.40  0.0
	load 123  	0.0 -182.40   0.0
	load 124  	0.0 -91.20   0.0
	load 131  	0.0 -35.75   0.0
	load 132  	0.0 -71.50   0.0
	load 133  	0.0 -71.50    0.0
	load 134  	0.0 -35.75   0.0
	
eleLoad -ele 521 522 523 524 531 532 533 534 -type -beamUniform  0.0 $self_pilastri 0.0
}
# Apply the load
# 	constraints Transformation
constraints 	Penalty 1e14 1e14
numberer 		RCM
system UmfPack -lvalueFact 30
# system UmfPack Mumps
test EnergyIncr	1e-6 1000
algorithm KrylovNewton
set nG 500;
integrator LoadControl [expr 1/$nG]
analysis Static
analyze $nG
# maintain constant gravity loads and reset time to zero
loadConst -time 0.0
puts "Gravity Analysis Completed..........."
print ele 521 522 523 524 531 532 533 534 

puts "Model Built"


puts "Load Pattern Started";				# Text seen on interpreter
# Load Pattern Definition--------------------------------------------------------
pattern Plain 1 Linear { 
load 121 	 1.0 0.0 0.0;	
load 122 	 1.0 0.0 0.0;		
load 123 	 1.0 0.0 0.0;	
load 124 	 1.0 0.0 0.0;	
load 131 	 2.0 0.0 0.0;	
load 132 	 2.0 0.0 0.0;		
load 133 	 2.0 0.0 0.0;	
load 134 	 2.0 0.0 0.0;
}
puts "Recorders Started";					# Text seen on interpreter
recorder Node -file ID_5_disp2_infill5.out -node 121 131 -dof 1 disp
# Support reactions
recorder Node -file ID_5_V2_infill5.out -node 11 12 13 14 -dof 1 reaction;
puts "Analysis Objects Started";			# Text seen on interpreter
constraints Plain;
numberer Plain;								# Nodes are assigned dof's arbitrarily
system BandGeneral;							# Un-symmetric banded system solved using Lapack Band General Solver
test NormDispIncr 1.0e-3 600 0;				# Create the convergence test
algorithm Newton;							# Create the solution algorithm
# integrator DisplacementControl $nodeTag $dofTag $dU1
integrator DisplacementControl 131 1 0.0001
analysis Static;							# Static Analysis
analyze 800000;									
puts "Done with Experimental test";		# Text seen on interpreter
wipe;

